#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
#define LENGTH 6
#define N 3

/* function declaration */

void rotate(int[], int);
void print_array(int[], int);
void unrepeated (int[], int);
void print_menu();
int sumatrix(int[][N], int, int);

/* main function */

int main(){
	int rotate_array[LENGTH] = {7,4,56,10,1,9};
	int unrepeated_array[SIZE] = {8,6,17,8,8,4,2,4,6,11};
	int matrix_arr[3][3] = {{1,4,6}, {7,0,9}};
	int key, cont = 1;
	print_menu();
	while  (cont != 0){
		scanf("%d", &key);
		switch (key){
			case 1:{
				system("cls");
				printf("Before Rotating:\n");
				print_array(rotate_array, LENGTH);
				rotate(rotate_array, LENGTH);
				printf("After Rotating:\n");
				print_array(rotate_array, LENGTH);
				print_menu();
				break;
				   }

			case 2:{
				system("cls");
				printf("Before checking repeated values:\n");
				print_array(unrepeated_array, SIZE);
				unrepeated(unrepeated_array, SIZE);
				print_menu();
				break;
				   }
			case 3:{
				system("cls");
				printf("Total: %d\n\n", sumatrix(matrix_arr, N, N));
				print_menu();
				break;
				   }
			default:{
				system("cls");
				return 0;
					}
		}
	}
}


void print_menu(){
	printf("Welcome to Exercise 6\n");
	printf("1- Cyclic Array\n");
	printf("2- Unrepeated Array\n");
	printf("3- Matrix Summ\n");
	printf("0- Exit\n");
}
/* Rotate Function */
//Rotate the array by saving in temp integer, the last value
// in array.
void rotate(int arr[], int size){
	int temp, i;
	temp = arr[size-1];
	for(i = size-1 ; i > 0 ; i--){
		arr[i] = arr[i-1];
	}
	arr[0] = temp;
}
void print_array(int arr[], int size){
	int i;
	for( i = 0; i < size; i++){
		printf("%d, ", arr[i]);
	}
	printf("\n\n");
}
void unrepeated(int arr[], int size){
	int temp[SIZE] = {0}, i, j, flag=0, counter = 0;
	for(i = 0; i < size; i++){
		for(j =0; j < size; j++){
			if (arr[i] == temp[j]){
				flag = 1; 
				break;
			}
		}
		if(flag ==1){
			flag = 0;
			continue;
		}
		temp[counter] = arr[i];
		counter++;
	}
	printf("Size: %d\n", counter);
	print_array(temp, counter);
}
int sumatrix(int arr[][N], int rows, int columns){
	int i, j, sum = 0;
	for(i =0; i < columns; i++){
		for (j = 0; j < rows; j++){
			sum += arr[i][j];
		}
	}
	return sum;
}